package cn.circle.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.system.dao.ISysUserDao;
import cn.circle.system.model.SysCustomer;
import cn.circle.system.model.SysUser;


@Repository
public class SysUserDao extends BaseDaoImpl<SysUser> implements ISysUserDao{
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> datagrid() {
		// TODO Auto-generated method stub
		String hql = "from SysUser ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<SysUser> getUser(String id) {
		// TODO Auto-generated method stub
		
		String hql = "from SysUser where userId = '"+id+"'";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}


	
	public boolean removeAll(String depId) {
		// TODO Auto-generated method stub
		String hql = "update SysUser s set s.status = 0 where s.custId in ("+depId+") ";
		System.out.println(hql);
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	
	public boolean deleteAll(String id) {
		// TODO Auto-generated method stub
		String hql = "delete SysUser s where s.userId in ("+id+") ";
		System.out.println(hql);
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}
	
	public boolean updataAll(SysUser c) {
		// TODO Auto-generated method stub
		
			StringBuffer hql = new StringBuffer(
					"update SysUser s set ");
			
			//拼接修改条件
			if(c.getUserName() != null && !"".equals(c.getUserName())){
				hql.append("  s.userName = '"+c.getUserName()+"' ");
			}
			if(c.getNickName() != null && !"".equals(c.getNickName())){
				hql.append(" , s.nickName = '"+c.getNickName()+"' ");
			}
			if(c.getPhone() != null && !"".equals(c.getPhone())){
			hql.append(" , s.phone = '"+c.getPhone()+"' ");
			}
			if(c.getPassWord() != null && !"".equals(c.getPassWord())){
				hql.append(" , s.passWord = '"+c.getPassWord()+"' ");
			}
			if(c.getAddress() != null && !"".equals(c.getAddress() )){
				hql.append(" , s.address = '"+c.getAddress() +"' ");
			}
		    
			/*if(c.getSchool() != null && !"".equals(c.getSchool())){
				hql.append(" , s.school = '"+c.getSchool()+" '");
			}*/

			if(c.getStatus() != null && !"".equals(c.getStatus())){
				hql.append(" , s.status = "+c.getStatus()+" ");
			}

			hql.append(" where s.userId ="+c.getUserId()+" ");
			Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
			return (query.executeUpdate() > 0);
			
			}
	
}
